package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>SetmealController</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 24/06/2024 16:40.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    /**
     * 分页查询套餐信息
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 套餐名称
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public R<Page<Setmeal>> querySetmeal(@RequestParam(name = "page") int page,
                                         @RequestParam(name = "pageSize") int pageSize,
                                         @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        return setmealService.querySetmeal(page, pageSize, name);
    }

    /**
     * 根据id查询套餐信息
     * @param id 套餐id
     * @return 套餐信息
     */
    @GetMapping("/{id}")
    public R<SetmealDto> queryById(@PathVariable("id") Long id) {
        return setmealService.queryById(id);
    }

    /**
     * 新增套餐
     * @param dto 套餐信息
     * @return 是否新增成功
     */
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto dto) {
        return setmealService.addSetmeal(dto);
    }

    /**
     * 批量删除
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @DeleteMapping()
    public R<String> delteDishByIds(@RequestParam("ids") String ids) {
        return setmealService.delteDishByIds(ids);
    }

    /**
     * 批量起售/停售
     * @param status 状态，起售-1，停售-0
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable("status") Integer status, @RequestParam("ids") String ids) {
        return setmealService.changeStatus(status, ids);
    }

    /**
     * 更新套餐信息
     * @param dto 套餐信息
     * @return 是否成功
     */
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto dto) {
        return setmealService.updateSetmeal(dto);
    }

    /**
     * 根据分类查询套餐信息
     * @param categoryId 分类ID
     * @param status 是否有效
     * @return 套餐信息
     */
    @GetMapping("/list")
    public R<List<Setmeal>> queryByCategoryIdAndStatus(@RequestParam("categoryId") Long categoryId,
                                                       @RequestParam("status") Integer status) {
        return R.success(setmealService.list(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status)));
    }

}
